package cn.edu.cug.cs.gtl.gsm.item;


import cn.edu.cug.cs.gtl.common.Identifier;
import cn.edu.cug.cs.gtl.io.Storable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class MetaItem implements Storable {
    //分页数据库的基本名称
    String baseName;
    //一个数据表、或一个文件、或一个集合中可以存放的最大记录条数
    //或者说一本书中最大的页面数目，例如，我们可以规定一本书最大页为1000，
    //如果多于1000，则要分册成书。
    long collectionSize;
    int  pageSize; //页面的大小,单位为字节
    Identifier nextPageIdentifier;//下一个页面的ID
    List<Identifier> emptyPageIdentifiers;//空页面的ID集合

    public long getCollectionSize() {
        return collectionSize;
    }

    public void setCollectionSize(long collectionSize) {
        this.collectionSize = collectionSize;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Identifier getNextPageIdentifier() {
        return nextPageIdentifier;
    }

    public void setNextPageIdentifier(Identifier nextPageIdentifier) {
        this.nextPageIdentifier = nextPageIdentifier;
    }

    public List<Identifier> getEmptyPageIdentifiers() {
        return emptyPageIdentifiers;
    }

    public void setEmptyPageIdentifiers(List<Identifier> emptyPageIdentifiers) {
        this.emptyPageIdentifiers = emptyPageIdentifiers;
    }

    public String getBaseName() {
        return baseName;
    }

    public void setBaseName(String baseName) {
        this.baseName = baseName;
    }

    /**
     *
     * @param collectionSize
     * @param pageSize
     */
    public MetaItem(long collectionSize, int pageSize) {
        this.collectionSize = collectionSize;
        this.pageSize = pageSize;
        this.nextPageIdentifier =Identifier .create(0);
        this.emptyPageIdentifiers = new ArrayList<>();
        this.baseName = UUID.randomUUID().toString();
    }

    @Override
    public Object clone() {
        MetaItem i = new MetaItem(this.collectionSize,this.pageSize);
        i.nextPageIdentifier.copyFrom(this.nextPageIdentifier);
        i.baseName = new String(this.baseName);
        for(Identifier id : this.emptyPageIdentifiers){
            Identifier k = (Identifier)id.clone();
            i.emptyPageIdentifiers.add(k);
        }
        return null;
    }

    @Override
    public boolean load(DataInput dataInput) throws IOException {
        this.baseName=dataInput.readUTF();
        collectionSize = dataInput.readLong();
        pageSize = (int) dataInput.readLong();
        nextPageIdentifier.load(dataInput);
        int c = dataInput.readInt();
        emptyPageIdentifiers.clear();
        for(int i=0;i<c;++i){
            Identifier id = Identifier.create();
            id.load(dataInput);
            emptyPageIdentifiers.add(id);
        }
        return true;
    }

    @Override
    public boolean store(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(this.baseName);
        //long collectionSize;
        dataOutput.writeLong(collectionSize);
        //int  pageSize; //页面的大小,单位为字节
        dataOutput.writeLong(pageSize);
        //Identifier nextPageIdentifier;//下一个页面的ID
        nextPageIdentifier.store(dataOutput);
        int c = emptyPageIdentifiers.size();
        dataOutput.writeInt(c);
        if(c>0){
            //List<Identifier> emptyPageIdentifiers;//空页面的ID集合
            for(Identifier i: emptyPageIdentifiers)
                i.store(dataOutput);
        }
        return true;
    }

    /**
     * 计算页面存放的集合的序号，从0开始
     * @param page
     * @return
     */
    public int getCollectionOrder(Identifier page){
        return (int)(page.longValue()/getCollectionSize());
    }
}
